﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using YAPL.Language.Ast.Utils;

namespace YAPL.Language.Ast {
	public class ClassDefExpression : Expression {
		private readonly string _className;
		private CodeBlockExpression _classBody;
		public ClassDefExpression(ExpressionType nodeType, Type type, string className, CodeBlockExpression classBody) : base(nodeType, type) {
			_className = className;
			_classBody = classBody;
		}

		public ClassDefExpression(ExpressionType nodeType, Type type, bool canRead, bool canWrite, string className, CodeBlockExpression classBody)
			: base(nodeType, type, canRead, canWrite) {
			_className = className;
			_classBody = classBody;
		}

		public string ClassName {
			get { return _className; }
		}

		[Descendant(AstVisit.AfterCurrent)]
		public CodeBlockExpression ClassBody {
			get { return _classBody; }
		}

		public override bool NodeEquals(Expression other) {
			if (other is ClassDefExpression) {
				var name = (other as ClassDefExpression)._className;
				return _className.Equals(name);
			}
			return false;
		}

		public override bool RewriteChild(Expression child, Expression newExpression) {
			if (ReferenceEquals(_className, child)) {
				_classBody = newExpression as CodeBlockExpression;
				return true;
			}

			return false;
		}
	}
}
